
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# DISCLAIMER AND GENERAL INFORMATION
#
# File: App_B_cbam_personal_regional_country.R
# Purpose: Appendix file for personal, regional, and country-wide effects
# Date: 10 July 2024
# Data: Survey data (pulled through 0_cbam_prep.R)
#
# Technical disclaimer:
# All analyses in R version 4.4.1 (2024-06-14 ucrt) -- "Race for Your Life"
# R Studio 2024.04.2 Build 764 ("Chocolate Cosmos" Release (e4392fc9, 2024-06-05) for Windows)
# Windows 10 Enterprise, 64-bit
# 12th Gen Intel(R) Core(TM) i7-1255U 1.70 GHz with 16GB RAM
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (A) Load data and packages ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Run source file to load data
source("0_cbam_prep.R")

# Load packages
library("tidyverse")
library("janitor")
library("ggpubr")
library("modelsummary")
options(modelsummary_format_numeric_latex = "plain")
library("marginaleffects")
library("xtable")
library("tinytable")


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (B) Experimental results: Impact on person ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Regression models: collapsed treatment conditions
ols_de <- lm(cbam_person_inv~factor(tr_main), data=dta[dta$country=="germany",]) 
ols_hu <- lm(cbam_person_inv~factor(tr_main), data=dta[dta$country=="hungary",]) 
ols_ch <- lm(cbam_person_inv~factor(tr_main), data=dta[dta$country=="switzerland",]) 
ols_uk <- lm(cbam_person_inv~factor(tr_main), data=dta[dta$country=="uk",]) 

order_full <- c("factor(tr_main)2"="Treatment: Jobs",
                "factor(tr_main)3"="Treatment: Prices",
                "factor(tr_main)4"="Treatment: Both")  

models.personal <- list(
  "Germany" = ols_de,
  "Hungary" = ols_hu,
  "Switzerland" = ols_ch,
  "UK" = ols_uk)

# Plot coefficients

# Set color-blind friendly color palatte
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

p1 <- modelplot(rev(models.personal), coef_map=order_full, coef_omit="Interc", facet=TRUE) +
  labs(x="Coefficients",
       y="",
       title="Treatment Effects across Countries",
       subtitle="DV: CBAM effect on person (1-5 scale)") +
  geom_vline(xintercept=0, color='black', linetype = "dashed") +
  theme_classic() +
  scale_color_manual(values = cols) +
  theme(legend.title=element_blank())
p1


# Export plot to PDF (Figure B.1)
ggexport(p1, filename="./Exp_impacts_personal.pdf", width=10, height=5)

# Table B.1
tab <- modelsummary(models.personal,
             statistic = 'conf.int',
             conf_level = .95,
             coef_rename = order_full,
             gof_map=c("nobs"),
             title=c("Regression results for CBAM effect on person"),
             output="tinytable") %>%
             theme_tt("placement", latex_float="h!")

tinytable::save_tt(tab, "./main_cbam_person.tex", overwrite=TRUE)



# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (C) Experimental results: Impact on region ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Regression models: collapsed treatment conditions
ols_de <- lm(cbam_region_inv~factor(tr_main), data=dta[dta$country=="germany",]) 
ols_hu <- lm(cbam_region_inv~factor(tr_main), data=dta[dta$country=="hungary",]) 
ols_ch <- lm(cbam_region_inv~factor(tr_main), data=dta[dta$country=="switzerland",]) 
ols_uk <- lm(cbam_region_inv~factor(tr_main), data=dta[dta$country=="uk",]) 

order_full <- c("factor(tr_main)2"="Treatment: Jobs",
                "factor(tr_main)3"="Treatment: Prices",
                "factor(tr_main)4"="Treatment: Both")  

models.regional <- list(
  "Germany" = ols_de,
  "Hungary" = ols_hu,
  "Switzerland" = ols_ch,
  "UK" = ols_uk)

# Plot coefficients

# Set color-blind friendly color palatte
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

p1 <- modelplot(rev(models.regional), coef_map=order_full, coef_omit="Interc", facet=TRUE) +
  labs(x="Coefficients",
       y="",
       title="Treatment Effects across Countries",
       subtitle="DV: CBAM effect on region (1-5 scale)") +
  geom_vline(xintercept=0, color='black', linetype = "dashed") +
  theme_classic() +
  scale_color_manual(values = cols) +
  theme(legend.title=element_blank())
p1


# Export plot to PDF (Figure B.2)
ggexport(p1, filename="./Exp_impacts_region.pdf", width=10, height=5)

# Table B.2
tab <- modelsummary(models.regional,
             statistic = 'conf.int',
             conf_level = .95,
             coef_rename = order_full,
             gof_map=c("nobs"),
             title=c("Regression results for CBAM effect on region"),
             output="tinytable") %>%
             theme_tt("placement", latex_float="h!")

tinytable::save_tt(tab, "./main_cbam_region.tex", overwrite=TRUE)


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (D) Experimental results: Impact on country ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Regression models: collapsed treatment conditions
ols_de <- lm(cbam_country_inv~factor(tr_main), data=dta[dta$country=="germany",]) 
ols_hu <- lm(cbam_country_inv~factor(tr_main), data=dta[dta$country=="hungary",]) 
ols_ch <- lm(cbam_country_inv~factor(tr_main), data=dta[dta$country=="switzerland",]) 
ols_uk <- lm(cbam_country_inv~factor(tr_main), data=dta[dta$country=="uk",])


order_full <- c("factor(tr_main)2"="Treatment: Jobs",
                "factor(tr_main)3"="Treatment: Prices",
                "factor(tr_main)4"="Treatment: Both")  

models.country <- list(
  "Germany" = ols_de,
  "Hungary" = ols_hu,
  "Switzerland" = ols_ch,
  "UK" = ols_uk)

# Plot coefficients

# Set color-blind friendly color palatte
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

p1 <- modelplot(rev(models.country), coef_map=order_full, coef_omit="Interc", facet=TRUE) +
  labs(x="Coefficients",
       y="",
       title="Treatment Effects across Countries",
       subtitle="DV: CBAM effect on country (1-5 scale)") +
  geom_vline(xintercept=0, color='black', linetype = "dashed") +
  theme_classic() +
  scale_color_manual(values = cols) +
  theme(legend.title=element_blank())
p1


# Export plot to PDF (Figure B.3)
ggexport(p1, filename="./Exp_impacts_country.pdf", width=10, height=5)

# Table B.3
tab <- modelsummary(models.country,
             statistic = 'conf.int',
             conf_level = .95,
             coef_rename = order_full,
             gof_map=c("nobs"),
             title=c("Regression results for CBAM effect on country as a whole"),
             output="tinytable") %>%
             theme_tt("placement", latex_float="h!")

tinytable::save_tt(tab, "./main_cbam_country.tex", overwrite=TRUE)
             
             


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                           END HERE
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
